package com.campus.dao.impl;

import com.campus.dao.TeachingEvaluationDao;
import com.campus.model.TeachingEvaluation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Service
@Slf4j
public class TeachingEvaluationDaoImpl implements TeachingEvaluationDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public boolean addEvaluation(TeachingEvaluation evaluation) {
        if (!isValidStudentInClassId(evaluation.getStuInClassId())) {
            return false;
        }

        if (!isValidCourseId(evaluation.getCourseId())) {
            return false;
        }

        if (!isValidTeacherId(evaluation.getEvaluator())) {
            return false;
        }

        String sql = "INSERT INTO t_teaching_evaluation (stu_in_class_id, grade, evaluation, evaulated_time, course_id, evaluator) " +
                "VALUES (?, ?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql, evaluation.getStuInClassId(), evaluation.getGrade(),
                evaluation.getEvaluation(), new java.sql.Timestamp(System.currentTimeMillis()), evaluation.getCourseId(), evaluation.getEvaluator()) > 0;
    }

    @Override
    public TeachingEvaluation getEvaluationById(Integer id) {
        String sql = "SELECT * FROM t_teaching_evaluation WHERE id = ?";
        try {
            return jdbcTemplate.queryForObject(sql, new Object[]{id}, new RowMapper<TeachingEvaluation>() {
                @Override
                public TeachingEvaluation mapRow(ResultSet rs, int rowNum) throws SQLException {
                    return mapResultSetToEvaluation(rs);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<TeachingEvaluation> getEvaluationsByCourseAndStudent(Integer courseId, Integer stuInClassId) {
        String sql = "SELECT * FROM t_teaching_evaluation WHERE course_id = ? AND stu_in_class_id = ?";
        return jdbcTemplate.query(sql, new Object[]{courseId, stuInClassId}, new RowMapper<TeachingEvaluation>() {
            @Override
            public TeachingEvaluation mapRow(ResultSet rs, int rowNum) throws SQLException {
                return mapResultSetToEvaluation(rs);
            }
        });
    }

    private TeachingEvaluation mapResultSetToEvaluation(ResultSet rs) throws SQLException {
        return new TeachingEvaluation()
                .setId(rs.getInt("id"))
                .setStuInClassId(rs.getInt("stu_in_class_id"))
                .setGrade(rs.getString("grade"))
                .setEvaluation(rs.getString("evaluation"))
                .setEvaluatedTime(rs.getTimestamp("evaulated_time"))
                .setCourseId(rs.getInt("course_id"))
                .setEvaluator(rs.getInt("evaluator"));
    }

    // Check if the student in class ID is valid
    private boolean isValidStudentInClassId(int studentInClassId) {
        String sql = "SELECT 1 FROM t_teaching_student_in_class WHERE id = ?";
        try {
            Integer count = jdbcTemplate.queryForObject(sql, new Object[]{studentInClassId}, Integer.class);
            return count != null && count > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    // Check if the teacher ID is valid
    private boolean isValidTeacherId(int teacherId) {
        String sql = "SELECT 1 FROM t_manager_teacher WHERE id = ?";
        try {
            Integer count = jdbcTemplate.queryForObject(sql, new Object[]{teacherId}, Integer.class);
            return count != null && count > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    // Check if the course ID is valid
    private boolean isValidCourseId(int courseId) {
        String sql = "SELECT 1 FROM t_course WHERE id = ?";
        try {
            Integer count = jdbcTemplate.queryForObject(sql, new Object[]{courseId}, Integer.class);
            return count != null && count > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
